<?php

namespace app\admin\command;

use app\admin\model\Reserve;
use app\admin\model\Room;
use app\admin\model\Cleanroom;
use app\common\library\Yunding;
use app\common\library\Reserve as ReserveLib;
use Carbon\Carbon;
use think\console\Command;
use think\console\Input;
use think\console\input\Option;
use think\console\Output;
use think\Db;
use think\Exception;
use think\Log;

class Elemetor extends Command
{

    protected function configure()
    {
        $this->setName('elemetor_control')
            ->setDescription('elemetor control');
    }

    protected function execute(Input $input, Output $output)
    {
        @ini_set("memory_limit", '-1');
        $stime = microtime(true);

        //得到要处理的教室
        $rooms = Room::all();

        $yunding = new Yunding();

        foreach ($rooms as $room) {
            sleep(2);
            echo $room->room_name . PHP_EOL;
            //判断是否有配置homeid和uuid, 没有配置，不做操作
            if (empty($room->yunding_homeid) or empty($room->yunding_elemetor_uuid)) {
                continue;
            }
            // 如果当前教室在cleanroom表中，半小时内不操作，直接跳过
            $isCleaning = Cleanroom::where('room_id', $room->id)
                ->where('opentime', '<', Carbon::now()->toDateTimeString())
                ->where('closetime', '>', Carbon::now()->toDateTimeString())
                ->count();
            if ($isCleaning) {
                echo $room->room_name . ' is cleaning' . PHP_EOL;   
                continue;
            }

            //得到电表的当前状态
            $isOn = $yunding->isElemeterSwitchOn($room->yunding_homeid, $room->yunding_elemetor_uuid);
            echo 'current: ';
            echo $isOn ? 'on' : 'off';
            echo PHP_EOL;

            //当前时间是否有预约， 在预约中，不做任何操作
            $isRunning = Reserve::where('starttime', '<', Carbon::now()->toDateTimeString())
                ->where('endtime', '>', Carbon::now()->toDateTimeString())
                ->where('room_id', $room->id)
                ->where('re_status', 1)
                ->count();

            if ($isRunning) {
                echo 'is runing' . PHP_EOL;
                if ($isOn) {
                } else {
                    $yunding->elemeterSwitchOn($room->yunding_homeid, $room->yunding_elemetor_uuid);
                    if (!empty($room->yunding_elemetor_uuid2)) {
                        $yunding->elemeterSwitchOn($room->yunding_homeid, $room->yunding_elemetor_uuid2);
                    }
                }
                continue;
            }

            //5,15,25,35,45,55
            //得到此教室未来12分钟内是否有预约， 有预约， 通电，没有预约，关电
            $wiilRunning = Reserve::where('starttime', '<', Carbon::now()->addMinutes(12)->toDateTimeString())
                ->where('endtime', '>', Carbon::now()->addMinutes(12)->toDateTimeString())
                ->where('room_id', $room->id)
                ->where('re_status', 1)
                ->count();

            if ($wiilRunning) { //开
                echo 'will runing: yes' . PHP_EOL;
                if ($isOn) {

                } else {
                    $yunding->elemeterSwitchOn($room->yunding_homeid, $room->yunding_elemetor_uuid);
                    if (!empty($room->yunding_elemetor_uuid2)) {
                        $yunding->elemeterSwitchOn($room->yunding_homeid, $room->yunding_elemetor_uuid2);
                    }
                }

            } else { //关
                echo 'will runing: no' . PHP_EOL;
                if ($isOn) {
                    $yunding->elemeterSwitchOff($room->yunding_homeid, $room->yunding_elemetor_uuid);
                    if (!empty($room->yunding_elemetor_uuid2)) {
                        $yunding->elemeterSwitchOff($room->yunding_homeid, $room->yunding_elemetor_uuid2);
                    }
                } else {

                }
            }
        }

        $etime = microtime(true);//获取程序执行结束的时间
        $total = $etime - $stime;   //计算差值
        $output->writeln($total);
    }

}
